package com.lalosoft.misgastos.config;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.lalosoft.misgastos.domain.Account;
import com.lalosoft.misgastos.domain.Item;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{

	// name of the database file for your application -- change to something
	// appropriate for your app
	private static final String DATABASE_NAME = "gastosDB.db";
	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 6;
	
	// the DAO object we use to access the User table
	private Dao<Item, Integer> itemDao = null;
	// the DAO object we use to access the Account table
	private Dao<Account, Integer> accountDao = null;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * This is called when the database is first created. Usually you should
	 * call createTable statements here to create the tables that will store
	 * your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		Log.i(DatabaseHelper.class.getName(), "onCreate");
		try {
			
			TableUtils.createTable(connectionSource, Item.class);
			itemDao = DaoManager.createDao(connectionSource, Item.class);
			Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: Item");
			
			TableUtils.createTable(connectionSource, Account.class);
			accountDao = DaoManager.createDao(connectionSource, Account.class);
			Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: Account");
			
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource,Item.class, true);
			
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
			
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource,Account.class, true);
			
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
			
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}
	
	public Dao<Item, Integer> getItemDao() throws SQLException {
		if (itemDao == null) {
			itemDao = getDao(Item.class);
		}
		return itemDao;
	}
	
	public Dao<Account, Integer> getAccountDao() throws SQLException{
		if(accountDao == null){
			accountDao = getDao(Account.class);
		}
		return accountDao;
	}
	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
		itemDao = null;
		accountDao = null;
	}
}
